<?
	require_once("class.curl.php");
	
	function get_providers($zipcode) {
		// scrape provider listing
		$curl = new curls4girls("");
		$curlurl = "http://tvlistings.zap2it.com/tvlistings/ZCGrid.do?zipcode=".$zipcode;
		$result = $curl->get($curlurl);
	
		// check for bad zip code
		if(strpos($result,"You have entered an invalid ZIP or postal code.") > 0)
			return false;
			
		// put providers into an array
		$chunk = piece($result, '<div class="zc-provider-list">','<!-- footer -->');
		$regexp = "#<a.*href\s*=\s*(\"|')?([^\"'>]+).*>([^<>]+)</a>#i";
		preg_match_all($regexp, $chunk, $matches);
	
		foreach($matches[3] as $x => $provider)
		{
			$provider_id = explode('=',$matches[2][$x]);	
			$f_array[] = array("id" => $provider_id[4], "name" => trim($provider));
		}
	
		return $f_array;
	}
	
	function get_listing_grid($provider) {
		// scrape tv listing grid
		$curl = new curls4girls("");
		$curlurl = "http://tvlistings.zap2it.com/tvlistings/ZCGrid.do?method=decideFwdForLineup&setMyPreference=false&lineupId=".$provider;
		$result = $curl->get($curlurl);
	
		// get time formatting data
		$time_zone = trim(piece($result,'"Time Zone">','</abbr>'));
		$time_start = trim(piece($result,'<div>','</div>',strpos($result,'zc-time-block')+18));

		// take out listing data
		$chunk = piece($result, 'name="1"', 'end zc content');
	
		// break apart each station
		$channels = explode('td class="zc-station', $chunk);
	
		foreach($channels as $channel) {
			$channel = explode("<td",$channel);
		
			for($c=0;$c<count($channel);$c++)
			{
				if($c == 0)
					$x['channel'] = parse_channel_item($channel[$c]);
				else
					$x['shows'][] = parse_show_item($channel[$c]);
			}
			
			// fix duration and time
			$ts_start = strtotime($time_start);
			$elapsed_time = 0;
			$total_time = 720;
			for($c=0;$c<count($x['shows']);$c++) {
				$x['shows'][$c]['time'] = $ts_start;
				
				if($x['shows'][$c]['length'] != '') {
					$elapsed_time+=$x['shows'][$c]['length']; 
					$x['shows'][$c]['length'] = $x['shows'][$c]['length']/4;
					$ts_start = $ts_start + ($x['shows'][$c]['length']/4)*60;
				} else {
					$x['shows'][$c]['length'] = ($total_time - $elapsed_time)/4;
				}
			}
			
			$grid[] = $x; $x = "";
		}
	
		array_shift($grid);
		
		return $grid;
	}
	
	function parse_channel_item($string)
	{
		$channel_array = array();
		
		$channel_array['number'] = piece($string,'zc-station-number">','</span>');
		$channel_array['icon'] = piece($string, 'src="', '" alt="');
		$channel_array['id'] = piece($string, 'id="', '"');
		$channel_array['name'] = piece($string, '">', '</a>', strpos($string,'zc-station-number">')+25);
		
		return $channel_array;
	}
	
	function parse_show_item($string)
	{
		$show_array = array();

		$show_array['desc'] = piece($string, 'zc-program-episode">', '</span>');
		$show_array['genre'] = piece($string, 'zc-genre-', '" id');
		$show_array['title'] = 	piece($string,'">', '</a>', strpos($string,'chn=')+5);
		$show_array['length'] = piece($string, 'width: ','px;');
	
		return $show_array;
	}
	
	function piece($string, $start, $end, $offset = 0)
	{	
		$s_pos = strpos($string, $start, $offset);
		
		if($s_pos != 0) {
			$e_pos = strpos($string, $end, $s_pos+strlen($start));
			return  substr($string, $s_pos+strlen($start), $e_pos-$s_pos-strlen($start));
		}
		
		else
			return "";
	}

?>